<template>
  <el-dialog
    title="支付宝提现"
    :visible="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
    width="460px"
    top="25vh"
  >
    <div class="income-popup">
      <el-form
        v-show="incomeVisible == 0"
        label-width="80px"
        :model="verifyForm"
        :rules="verifyRules"
        ref="verifyForm"
        size="small"
      >
        <el-form-item label="默认账号">
          <el-row type="flex">
            <el-input type="text" :value="phone | formatMask" :disabled="true"></el-input>
            <el-link :underline="false" type="primary" @click="handleLink">提现至其他账户</el-link>
          </el-row>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <div class="verify-code">
            <el-input type="text" v-model="verifyForm.code" placeholder="请输入验证码"></el-input>
            <EduVerificationCode
              btnType="text"
              @start="handleCode('verifyForm')"
            ></EduVerificationCode>
          </div>
        </el-form-item>
        <el-form-item label="真实姓名" prop="name">
          <el-input type="text" v-model="verifyForm.name" placeholder="请输入姓名"> </el-input>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="handleSubmit('verifyForm')" size="small"
            >立即提现</el-button
          >
        </el-row>
      </el-form>
      <!-- 其他账户提现 -->
      <el-form
        v-show="incomeVisible == 1"
        label-width="80px"
        :model="incomeForm"
        :rules="incomeRules"
        ref="incomeForm"
        size="small"
      >
        <el-form-item label="真实姓名" prop="name">
          <el-row type="flex">
            <el-input
              type="text"
              v-model="incomeForm.name"
              placeholder="请输入支付宝真实姓名"
            ></el-input>
          </el-row>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input
            type="text"
            v-model="incomeForm.account"
            placeholder="请输入支付宝账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input type="text" :value="phone | formatMask" :disabled="true"> </el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <div class="verify-code">
            <el-input type="text" v-model="incomeForm.code" placeholder="请输入验证码"></el-input>
            <EduVerificationCode
              btnType="text"
              @start="handleCode('incomeForm')"
            ></EduVerificationCode>
          </div>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="handleSubmit('incomeForm')" size="small"
            >立即提现</el-button
          >
        </el-row>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import EduVerificationCode from '@/components/EduVerificationCode'
export default {
  props: {
    visible: {
      type: Boolean
    },
    money: {
      type: Number
    }
  },
  inject: ['reload'],
  components: {
    EduVerificationCode
  },
  data() {
    return {
      phone: this.$store.getters['user/accountInfo'].phone,
      verifyRules: {
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 8, message: '请输入正确姓名', trigger: 'blur' }
        ]
      },
      incomeRules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 8, message: '请输入正确姓名', trigger: 'blur' }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      verifyForm: {
        code: '',
        name: ''
      },
      incomeForm: {
        phone: '',
        name: '',
        account: '',
        code: ''
      },
      incomeVisible: 0 // 0 我的账户 1 其他账户
    }
  },
  methods: {
    handleSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let query = {
            money: this.money
          }
          if (this.incomeVisible == 0) {
            query = {
              ...query,
              alipay_account: this.phone,
              alipay_user_name: this.verifyForm.name,
              verify_code: this.verifyForm.code
            }
          } else if (this.incomeVisible == 1) {
            query = {
              ...query,
              alipay_account: this.incomeForm.account,
              alipay_user_name: this.incomeForm.name,
              verify_code: this.incomeForm.code
            }
          }
          this.$api.createIncomeEncashmentAlipay(
            query,
            res => {
              this.$message.success(res.msg, res => {
                this.reload()
              })
            },
            error => {
              this.$message.error(error.msg)
            }
          )
        }
      })
    },
    // 获取验证码
    handleCode() {
      this.$api.createSmsVerify(
        {
          phone: this.phone,
          type: 'alipay'
        },
        res => {
          this.$message.success(res.msg)
        },
        error => {
          this.$message.error(error.msg)
        }
      )
    },
    handleLink() {
      this.incomeVisible = 1
      this.$refs['verifyForm'].resetFields()
    },
    // 关闭弹窗
    handleClose() {
      this.incomeVisible = 0
      this.$emit('update:visible', false)
      this.$refs['incomeForm'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid $border-color-divider;
}
.income-popup {
  .el-form {
    ::v-deep .el-input {
      width: 224px;
      .el-input__inner {
        border-radius: 0px;
      }
      .el-input-group__append {
        border-radius: 0px;
        padding: 0 10px;
        background-color: #fff;
        @include edu-color-auxiliary;
      }
      &.is-disabled .el-input__inner {
        background-color: #fff;
        color: $text-color-auxiliary;
        cursor: default;
      }
    }
    .el-link {
      margin-left: 10px;
    }
    .verify-code {
      width: 224px;
      border: 1px solid #dcdfe6;
      ::v-deep .el-input {
        width: 145px;
        .el-input__inner {
          border: 0;
        }
      }
    }
  }
}
</style>
